import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_test/test1.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.green),
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  var _image_xiangche;
  var _image_xiangji;
  var _image_sdcard;

  _imageXJ() async {
    var path =
        (await ImagePicker().pickImage(source: ImageSource.camera))?.path;
    setState(() {
      _image_xiangji = path ==null?null:File(path);
    });
  }

  _imageXC() async {
    var path =
        (await ImagePicker().pickImage(source: ImageSource.gallery))?.path;
    setState(() {
      _image_xiangche = path ==null?null:File(path);
    });
    return _image_xiangche;
  }

  _imageSD() async {
    _image_sdcard = (await getExternalStorageDirectory())?.path;
  }

  @override
  void setState(VoidCallback fn) {
    pri("value");
    // _imageXJ();
    // _imageXC();
    // _imageSD();
    super.setState(fn);
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
        BoxConstraints(
            maxWidth: MediaQuery.of(context).size.width,
            maxHeight: MediaQuery.of(context).size.height),
        designSize: Size(360, 720),
        orientation: Orientation.portrait);
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          children: [
            ElevatedButton(
              onPressed: _imageXC,
              child: Text("相册"),
              clipBehavior: Clip.antiAlias,
              style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.green),
                  shape: MaterialStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(15))))),
            ),
            _image_xiangche == null
                ? Text("请点击上面的按钮！")
                : Image.file(
                    _image_xiangche,
                    width: 100,
                    height: 100,
                  ),
            ElevatedButton(
              onPressed: _imageXJ,
              child: Text(
                "相机",
                textAlign: TextAlign.center,
              ),
              clipBehavior: Clip.antiAlias,
              style: ButtonStyle(
                  fixedSize: MaterialStateProperty.all(Size(100, 100)),
                  backgroundColor: MaterialStateProperty.all(Colors.green),
                  shape: MaterialStateProperty.all(CircleBorder())),
            ),
            _image_xiangji == null
                ? Text("请点击上面的按钮！")
                : Image.file(
                    _image_xiangji,
                    width: 100.w,
                    height: 100.h,
                  ),
            ElevatedButton(
                onPressed: () {},
                child: Text(
                  "ElevatedButton",
                  textAlign: TextAlign.center,
                ),
                clipBehavior: Clip.antiAlias,
                style: ElevatedButton.styleFrom()),
            ElevatedButton(
              onPressed: () {},
              child: InkWell(
                child: Text("1"),
                splashColor: Colors.green,
              ),
            ),
            TextButton(
              onPressed: () => {},
              child: Text("TextButton"),
              clipBehavior: Clip.antiAlias,
              style: ButtonStyle(),
            ),
            OutlinedButton(
              onPressed: () => {},
              child: Text(
                "OutlinedButton",
                style: TextStyle(color: Colors.white),
              ),
              style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.green),
                  splashFactory: InkSplash.splashFactory,
                  shape: MaterialStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(15))))),
            )
          ],
        ),
      ),
    );
  }
}
